package cn.xspace.wnx.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.adapter.BasePagerViewAdapter;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.view.DragImageView;

import java.io.File;
import java.util.ArrayList;

import com.xspace.android.xafcommon.base.BaseActivity;

import cn.xspace.library.util.BldNavigation;
import cn.xspace.wnx.R;
import cn.xspace.chatui.bean.MsgRecordEntity;

import static cn.xspace.wnx.activity.ImageLargerActivity.IMAGE_INDEX;
import static cn.xspace.wnx.activity.ImageLargerActivity.IMAGE_LIST;


public class ACT_ChartImageBrowse extends BaseActivity {
    public static final String EXTRA_IMAGW_INDEX = "image_index";
    public static final String EXTRA_IMAGW_ARRAY = "image_array";
    private TextView tv_index;
    public int detail = 0;
    private ImageOnClickListener imageOnClickListener = new ImageOnClickListener();
    public static final String EXTRAS_IMAGE_VOS = "imageVos";
    protected ViewPager vpImage;
    protected ArrayList<MsgRecordEntity> dataList;
    int window_width;
    int window_height;
    private int pageIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.im_image_browse);
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        //因需求图片个数不再显示
        tv_index = (TextView) findViewById(R.id.tv_index);
        vpImage = (ViewPager) findViewById(R.id.vpImage);
        tv_index.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        super.initData();
        WindowManager manager = getWindowManager();
        window_width = manager.getDefaultDisplay().getWidth();
        window_height = manager.getDefaultDisplay().getHeight();
        toUpdatePagerView();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        vpImage.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                //tv_index.setVisibility(View.VISIBLE);
                //tv_index.setText((position + 1) + "/" + imageVos.size());
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected void getIntentData() {
        super.getIntentData();
        Bundle mbunle = getIntent().getBundleExtra(BldNavigation.KEY_BUNDLER);
        dataList = (ArrayList<MsgRecordEntity>) mbunle.getSerializable(IMAGE_LIST);
        pageIndex = mbunle.getInt(IMAGE_INDEX, 0);
    }


    public int getCurrentPage() {
        return vpImage.getCurrentItem();
    }


    private void toUpdatePagerView() {
        PagerAdapter adapter = vpImage.getAdapter();
        if (adapter == null) {
            ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter();
            imagePagerAdapter.setListData(dataList);
            vpImage.setAdapter(imagePagerAdapter);
            vpImage.setCurrentItem(pageIndex);
        } else {
            ImagePagerAdapter imageAdapter = (ImagePagerAdapter) adapter;
            imageAdapter.setListData(dataList);
            imageAdapter.notifyDataSetChanged();
        }
    }

    private class ImageOnClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            onImageClick(v);
        }
    }

    class ImagePagerAdapter extends BasePagerViewAdapter<MsgRecordEntity> {
        public ImagePagerAdapter() {
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MsgRecordEntity bean = getItem(position);
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_pager_image, parent, false);
            }
            final DragImageView imageView = (DragImageView) convertView.findViewById(R.id.image);
            imageView.setOnClickListener(imageOnClickListener);
            imageView.setOnLongClickListener(imageOnLongClickListener);
            imageView.setTag(position);
            imageView.setScreen_H(window_height);
            imageView.setScreen_W(window_width);
            imageView.setmActivity(ACT_ChartImageBrowse.this, true);
            final ProgressBar spinner = (ProgressBar) convertView.findViewById(R.id.loading);


            if (new File(bean.mFileEntity.file_url_local).exists()) {
                LogUtil.d(bean.mFileEntity.file_url_local);
                Picasso.with(mContext)
                        .load(new File(bean.mFileEntity.file_url_local))
                        .placeholder(R.drawable.chat_default_image)
                        .error(R.drawable.chat_default_image_error)
                        .into(imageView);
            } else if (!TextUtils.isEmpty(bean.mFileEntity.file_url_remote)) {
                LogUtil.d(bean.mFileEntity.file_url_remote);
                Picasso.with(mContext)
                        .load(bean.mFileEntity.file_url_remote)
                        .placeholder(R.drawable.chat_default_image)
                        .error(R.drawable.chat_default_image_error)
                        .into(imageView);
            } else {
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.chat_default_image_error));
            }
            return convertView;
        }
    }

    public static final String EXTRAS_INDEX = "index";

    private void initImagePager() {
        vpImage = (ViewPager) findViewById(R.id.vpImage);
    }

    public void onImageClick(View view) {
        // LogUtil.v("imageView","onImageClick");
        onBackPressed();
    }

    private class ImageOnLongClickListener implements OnLongClickListener {

        @Override
        public boolean onLongClick(View v) {
            onImageLongClick(v);
            return true;
        }
    }

    private ImageOnLongClickListener imageOnLongClickListener = new ImageOnLongClickListener();

    protected void onImageLongClick(View v) {
        //		Bitmap targetBitmap = null;
        //
        //		Integer position = (Integer) v.getTag();
        //		if (imageVos != null && position < imageVos.size()) {
        //			String vo = imageVos.get(position);
        //			String url = StringUtils.getUri(vo);
        //			ImageSize targetSize = new ImageSize(widthPixels, heightPixels);
        //			String memoryCacheKey = MemoryCacheUtils.generateKey(url, targetSize);
        //			targetBitmap =ImageLoader.getInstance().getMemoryCache().get(memoryCacheKey);
        //		}
        //
        //		if (targetBitmap == null) {
        //			Toast.makeText(this, "保存图片未加载成功！", Toast.LENGTH_LONG).show();
        //			return;
        //		}
        //		if (twoButtonPopupWindow == null) {
        //			twoButtonPopupWindow = new TwoButtonPopupSavaImg(this, targetBitmap);
        //		}
        // 显示窗口：设置layout在PopupWindow中显示的位置
        //		twoButtonPopupWindow.showAtLocation(findViewById(R.id.fl_large_image_browse), Gravity.BOTTOM, 0, 0);
    }

}
